<?php
// DSN function
function zeus_dsn()
{
    $dsn = '';
    switch (DB_TYPE) {
        // DSN for MySql 3.x/4.x/5.x
        case 'MYSQL':
            $dsn = 'mysql:host=' . DB_HOST . ';dbname=' . DB_NAME . ';port=' . DB_PORT;
            break;
        // DSN for FreeTDS/Microsoft SQL Server/Sybase
        case 'MSSQL':
            $dsn = 'mssql:host=' . DB_HOST . ';dbname=' . DB_NAME ;
            break;
        case 'SYBASE':
            $dsn = 'sybase:host=' . DB_HOST . ';dbname=' . DB_NAME ;
            break;
        case 'DBLIB':
            $dsn = 'dblib:host=' . DB_HOST . ';dbname=' . DB_NAME ;
            break;
        // TO DO：
        // DSN for Firebird/Interbase 6
        // DSN for IBM DB2
        // DSN for IBM Informix Dynamic Server
        // DSN for Oracle Call Interface
        // DSN for ODBC v3 (IBM DB2, unixODBC and win32 ODBC)
        // DSN for Oracle Call Interface
        // DSN for PostgreSQL
        // DSN for SQLite 3 and SQLite 2
        case 'SQLITE2':
            $dsn = 'sqlite2:' . DB_URI;
            break;
        case 'SQLITE3':
            $dsn = 'sqlite:' . DB_URI;
            break;
        default:
            $dsn = 'mysql:host=' . DB_HOST . ';dbname=' . DB_NAME . ';port=' . DB_PORT;
            break;
    }
    return $dsn;
}
// PDO SQL query
function zeus_sql_query($sql = '', $debug = false, $fetchObj = 'Alone')
{
    if ($debug) {
        echo "Query string: " . $sql . ";<BR>";
    } else {
        zeus_do_nothing();
    }
    $dsn = zeus_dsn();
    switch (DB_TYPE) {
        case 'MYSQL':
            $db = new PDO($dsn, DB_USER, DB_PASSWORD);
            break;
        default:
            $db = new PDO($dsn, DB_USER, DB_PASSWORD);
    }
    $db->query('SET NAMES UTF8');
    if ("" != $sql) {
        $result = ('All' == $fetchObj)?($db->query($sql)->fetchAll()):($db->query($sql)->fetch());
    } else {
        $result = false;
    }
    unset($db);
    return $result;
}
// PDO SQL execute
function zeus_sql_exec($sql = '', $debug = false)
{
    if ($debug) {
        echo "Query string: " . $sql . ";<BR>";
    } else {
        zeus_do_nothing();
    }
    $dsn = zeus_dsn();
    switch (DB_TYPE) {
        case 'MYSQL':
            $db = new PDO($dsn, DB_USER, DB_PASSWORD);
            break;
        default:
            $db = new PDO($dsn, DB_USER, DB_PASSWORD);
            break;
    }
    $db->query('SET NAMES UTF8');
    $count = $db->exec($sql);
    unset($db);
    return $count;
}
// PDO SQL prepare
function zeus_sql_prepare($sql = '', $debug = false, $parameters = array())
{
    if ($debug) {
        echo "Query string: " . $sql . ";<BR>";
    } else {
        zeus_do_nothing();
    }
    $dsn = zeus_dsn();
    switch (DB_TYPE) {
        case 'MYSQL':
            $db = new PDO($dsn, DB_USER, DB_PASSWORD);
            break;
        default:
            $db = new PDO($dsn, DB_USER, DB_PASSWORD);
            break;
    }
    $db->query('SET NAMES UTF8');
    $exec = $db->prepare($sql);
    foreach($parameters AS $parameter) {
        $exec->execute($parameter);
    }
    unset($db);
    return true;
}

?>